package com.east.controller.task;

import com.east.bean.Task;
import com.east.bean.TaskImage;
import com.east.bean.User;
import com.east.service.task.TaskService;
import com.east.utils.*;
import com.github.pagehelper.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("task")
public class TaskController {
    @Autowired
    private TaskService taskService;

    /**
     * 分页查询全部
     * @param pagenation
     * @return
     */
    @GetMapping("all")
    @RequiresPermissions("task:all")
    public Result findAll(Pagenation pagenation){
    	User token = BaseUtil.Token();
        Page<Task> tasks = taskService.selectAll(pagenation,token);
        Object pageJson = new PageJson<Task>().pageJson(tasks);
        return ResultUtil.success(pageJson);
    }
    /**
     * 按条件（关键字，项目，类型，工作量，指派人，完成状态）分页查询
     * @param pagenation
     * @return
     */
    @GetMapping("findBy")
    @RequiresPermissions("task:findBy")
    public Result findBy(Pagenation pagenation, Task task){
        Page<Task> tasks = taskService.selectBy(pagenation,task);
        Object pageJson = new PageJson<Task>().pageJson(tasks);
        return ResultUtil.success(pageJson);
    }

    /**
     * 新增任务分配
     * @param task
     * @return
     */
    @PostMapping("save")
    @RequiresPermissions("task:save")
    public Result save(@RequestBody Task task){
        int insert = taskService.insert(task);
        if (insert>0){
            return ResultUtil.success("新增成功");
        }else{
            return ResultUtil.error(500,"新增失败");
        }
    }

    /**
     * 个人添加任务
     * @param task
     * @return
     */
    @PostMapping("saveByOwn")
    @RequiresPermissions("task:saveByOwn")
    public Result saveByOwn(@RequestBody Task task){
    	User token = BaseUtil.Token();
        int insert = taskService.insertByOwn(task,token);
        if (insert>0){
            return ResultUtil.success("新增成功");
        }else{
            return ResultUtil.error(500,"新增失败");
        }
    }

    /**
     * 修改任务分配
     * @param task
     * @return
     */
    @PostMapping("update")
    @RequiresPermissions("task:update")
    public Result update(@RequestBody Task task){
        int update = taskService.update(task);
        if (update>0){
            return ResultUtil.success("修改成功");
        }else{
            return ResultUtil.error(500,"修改失败");
        }
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @DeleteMapping("delete")
    @RequiresPermissions("task:delete")
    public Result delete(Integer[] ids){
        int delete = taskService.delete(ids);
        if (delete>0){
            return ResultUtil.success("删除成功");
        }else{
            return ResultUtil.error(500,"删除失败");
        }
    }

    /**
     * 修改状态为已完成
     * @param task
     * @return
     */
    @PostMapping("updateByStatus")
    @RequiresPermissions("task:updateByStatus")
    public Result updateByStatus(Task task){
        int update = taskService.updateByState(task);
        if (update>0){
            return ResultUtil.success("已完成");
        }else{
            return ResultUtil.error(500,"修改失败");
        }
    }
    /**
     * 统计数量   状态tState（0.未完成  1.已完成）
     * @param task
     * @return
     */
    @GetMapping("countByStatus")
    @RequiresPermissions("task:countByStatus")
    public Result countByStatus(Task task){
        int count = taskService.countByStatus(task);
        return  ResultUtil.success(count);

    }
    /**
     * 统计各部门工作量总和
     * @param
     * @return
     */
    @GetMapping("sumWork")
    @RequiresPermissions("task:sumWork")
    public Result sumWork(){
        int count = taskService.sumWork();
        return  ResultUtil.success(count);

    }


    /**
     * 类型情况（项目名称，项目总工作量）
     */
    @GetMapping("countByType")
    @RequiresPermissions("task:countByType")
    public Result countByType(){
        List<Task> task = taskService.selectPro();
        return  ResultUtil.success(task);

    }
    /**
     * 按类型查询工作量(项目合计工作量，维护合计工作量)
     */
    @GetMapping("sumWorkByType")
    @RequiresPermissions("task:sumWorkByType")
    public Result sumWorkByType(){
        List<Task> task = taskService.selectSumWork();
        return  ResultUtil.success(task);

    }

    /**
     * 多文件上传(新增时不传tId，修改时传tId)
     * @param
     * @return "data":文件存储的子路径; 格式: 2019/0312/时间戳+序号+.后缀
     * @throws IOException
     */
    @PostMapping("/uploadfile")
    public Result uploadFile(@RequestParam("file") MultipartFile[] file, @RequestParam(value = "tId",required = false) Integer tId) {
        try {
            List<TaskImage> taskImages = taskService.uploadTaskImage(file, tId);
            if (taskImages!=null){
                return ResultUtil.success(taskImages);
            }else{
                return ResultUtil.error(500,"上传失败",null);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return  ResultUtil.error(500,"上传失败");
        }

    }

    /**
     * 删除本地文件
     * @param url
     * @return
     */
    @DeleteMapping("/deletefile")
    public Result delFile(String url) {
        if(StringUtils.isNotBlank(url)){
            int deletepho = taskService.deletepho(url);
            if (deletepho>0){
                return ResultUtil.success("删除成功");
            }else {
                return ResultUtil.error(500,"删除失败");
            }
        }else{
            return ResultUtil.error(500,"删除失败");
        }


    }

    /**
     * 根据tId查看详情
     */
    @GetMapping("selectBytId")
    @RequiresPermissions("task:selectBytId")
    public Result selectBytId(Task task){
        Task task1 = taskService.selectBytId(task);
        return  ResultUtil.success(task1);



    }



}
